package com.算法.操作系统;

/**
 * @author zh
 * @date 2024-01-01 17:41
 */
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

public class LoginForm extends JFrame {

    private static final long serialVersionUID = 1L;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new LoginForm());
    }

    public LoginForm() {
        initialize();
    }

    private void initialize() {
        setTitle("Login Form");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(300, 200);
        setLocationRelativeTo(null);

        Container container = getContentPane();
        container.setLayout(new FlowLayout());

        // Create components
        JLabel usernameLabel = new JLabel("Username: ");
        JTextField usernameField = new JTextField(15);
        JLabel passwordLabel = new JLabel("Password: ");
        JPasswordField passwordField = new JPasswordField(15);
        JButton submitButton = new JButton("Submit");
        JButton resetButton = new JButton("Reset");

        // Set border for components
        usernameLabel.setBorder(new EmptyBorder(5, 5, 5, 5));
        passwordLabel.setBorder(new EmptyBorder(5, 5, 5, 5));
        usernameField.setBorder(new CompoundBorder(
                new LineBorder(Color.GRAY, 1),
                new EmptyBorder(5, 5, 5, 5)));
        passwordField.setBorder(new CompoundBorder(
                new LineBorder(Color.GRAY, 1),
                new EmptyBorder(5, 5, 5, 5)));

        // Add listeners to buttons
        submitButton.addActionListener(e -> handleSubmit(usernameField.getText(), String.valueOf(passwordField.getPassword())));
        resetButton.addActionListener(e -> resetFields(usernameField, passwordField));

        // Create panel to hold labels and text fields
        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new GridLayout(2, 2, 10, 10));
        centerPanel.add(usernameLabel);
        centerPanel.add(usernameField);
        centerPanel.add(passwordLabel);
        centerPanel.add(passwordField);

        // Create panel to hold buttons
        JPanel southPanel = new JPanel();
        FlowLayout layout = new FlowLayout(FlowLayout.RIGHT);
        layout.setHgap(10);
        southPanel.setLayout(layout);
        southPanel.add(submitButton);
        southPanel.add(resetButton);

        // Add panels to frame
        container.add(centerPanel, BorderLayout.CENTER);
        container.add(southPanel, BorderLayout.SOUTH);

        setVisible(true);
    }

    private void handleSubmit(String username, String password) {
        boolean success = validateCredentials(username, password);
        if (!success) {
            JOptionPane.showMessageDialog(this, "Invalid username or password.", "Login Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void resetFields(JTextField field1, JPasswordField field2) {
        field1.setText("");
        field2.setText("");
    }

    private boolean validateCredentials(String username, String password) {
        // TODO: Implement actual validation logic here
        return true;
    }
}
